<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */

class b2c_ctl_admin_brand extends desktop_controller{

    var $workground = 'b2c.workground.goods';


    function index(){
        $this->finder('b2c_mdl_brand',array(
            'title'=>app::get('b2c')->_('商品品牌'),
            'actions'=>array(
                array('label'=>app::get('b2c')->_('添加品牌'),'icon'=>'add.gif','href'=>'index.php?app=b2c&ctl=admin_brand&act=create','target'=>'_blank'),

            ),
			'use_buildin_filter'=>true,//是否启用高级筛选：true启用
            ));
    }

    function getCheckboxList(){
        $brand = $this->app->model('brand');
        $this->pagedata['checkboxList'] = $brand->getList('brand_id,brand_name',null,0,-1);
        $this->page('admin/goods/brand/checkbox_list.html');
    }

    function create(){
        $oGtype = $this->app->Model('goods_type');
        $objBrand = $this->app->model('brand');
        $bb_model = $this->app->model('business_brand');
        $this->pagedata['type'] = $objBrand->getDefinedType();
        $this->pagedata['brandInfo']['type'][$this->pagedata['type']['default']['type_id']] = 1;
        $this->pagedata['gtype']['status'] = $oGtype->checkDefined();
		//edit yhy
		$this->pagedata['brandInfo']['rebate']=10;
		$this->pagedata['brandInfo']['sto_confirm']='Y';
		$this->pagedata['brandInfo']['account_mode']='rebate';
        
        $this->singlepage('admin/goods/brand/detail.html');
    }

    function save(){
      $this->begin('index.php?app=b2c&ctl=admin_brand&act=index');
		//yhy时间转换时间戳
		$_POST['register_time']=strtotime($_POST['register_time']);
        $objBrand = $this->app->model('brand');
        $brandname = $objBrand->dump(array('brand_name'=>$_POST['brand_name'],'brand_id'));
        //修改编辑品牌时，品牌名称重复问题---jyy
        if(is_array($brandname) && (($_POST['brand_id']) != ($brandname['brand_id']))){
             $this->end(false,app::get('b2c')->_('品牌名重复'));
        }
        $_POST['ordernum'] = intval( $_POST['ordernum'] );
        $data = $this->_preparegtype($_POST);
        //-----liuyulei 自由定价 rebate至0------
        if($data['account_mode'] == 'freeprice') {
            $data['rebate'] = 0;
        }
        //print_r('<pre>');print_r($data);exit();
        //-----liuyulei 自由定价 rebate至0 end------

        #↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓记录管理员操作日志@lujy↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
        if($obj_operatorlogs = kernel::service('operatorlog.goods')){
            $olddata = app::get('b2c')->model('brand')->dump($_POST['brand_id']);
        }

        //@kim 更改品牌状态的时候对照供应商进行goods的状态修改
        #↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑记录管理员操作日志@lujy↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
        if($objBrand->save($data)){
            //获取最新添加的id 用来记录到关联表中 edit by kim

            #↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓记录管理员操作日志@lujy↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
            if($obj_operatorlogs = kernel::service('operatorlog.goods')){
                if(method_exists($obj_operatorlogs,'brand_log')){
                    $obj_operatorlogs->brand_log($_POST,$olddata);
                }
            }
            #↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑记录管理员操作日志@lujy↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
            $this->end(true,app::get('b2c')->_('品牌保存成功'));
        }else{
            $this->end(false,app::get('b2c')->_('品牌保存失败'));
        } 
		
    }

    function edit($brand_id){

        $this->path[] = array('text'=>app::get('b2c')->_('商品品牌编辑'));
        $objBrand = $this->app->model('brand');
        $this->pagedata['brandInfo'] = $objBrand->dump($brand_id);
		//echo '<pre>';
		//print_r($this->pagedata['brandInfo']);
		//echo '</pre>';
        //if(empty($this->pagedata['brandInfo']['brand_url'])) $this->pagedata['brandInfo']['brand_url'] = 'http://';

        foreach($objBrand->getBrandTypes($brand_id) as $row){
            $aType[$row['type_id']] = 1;
        }

        $this->pagedata['brandInfo']['type'] = $aType;
        $this->pagedata['type'] = $objBrand->getDefinedType();
        $objGtype = $this->app->model('goods_type');
        $this->pagedata['gtype']['status'] = $objGtype->checkDefined();
        //brand_bind_business---edit by kim
        $oBusiness = $this->app->model('business_basicmsg');
        $bbModel = $this->app->model('business_brand');
        $business = $oBusiness->getList('business_id,russia_all_name');
        foreach($business as $k=>$v){
            $newarr['business'][$v['business_id']] = $v['russia_all_name'];
        }

        //依据品牌ID查询显示供应商ID
        $b = $bbModel->getList('business_id',array('brand_id'=>$brand_id));
        foreach ($b as $key => $value) {
            $b['business_id'][$key] = $value['business_id'];
        }
        //print_r($newarr);exit();
        $newarr['business_id'] = $b['business_id'];
        $this->pagedata['business'] = $newarr;
        //print_r($newarr);exit();
        $this->singlepage('admin/goods/brand/detail.html');
    }
    function _preparegtype($data){
        if(is_array($data['gtype'])){
            foreach($data['gtype'] as $key=>$val){
                $pdata = array('type_id'=>$val);
                $result[] = $pdata;
            }
        }
        $data['seo_info']['seo_title'] = $data['seo_title'];
        $data['seo_info']['seo_keywords'] = $data['seo_keywords'];
        $data['seo_info']['seo_description'] = $data['seo_description'];
        $data['seo_info'] = serialize($data['seo_info']);
        unset($data['seo_title']);
        unset($data['seo_keywords']);
        unset($data['seo_description']);
        $data['gtype'] = $result;
        return $data;
    }

}
